package org.microblog.service.impl;

import org.apache.ibatis.session.SqlSession;
import org.microblog.dao.FollowDao;
import org.microblog.dao.impl.FollowDaoImpl;
import org.microblog.entity.Follow;
import org.microblog.entity.User;
import org.microblog.service.FollowService;
import org.microblog.util.DatabaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
@Service("followService")
public class FollowServiceImpl implements FollowService {
    @Autowired
    private FollowDao followDao;

    public void setFollowDao(FollowDao followDao) {
        this.followDao = followDao;
    }

    @Override
    public int getFollowedCount(int uId) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession();
        //FollowDao dao=sqlSession.getMapper(FollowDao.class);
        int ret=followDao.getFollowedCount(uId);
       // sqlSession.commit();
       // sqlSession.close();
        return ret;
    }

    @Override
    public int getFansCount(int uId) throws SQLException {
       // SqlSession sqlSession=sessionFactory.openSession();
        //FollowDao dao=sqlSession.getMapper(FollowDao.class);
        //sqlSession.commit();
        int ret=followDao.getFansCount(uId);
       // sqlSession.close();
        return ret;
    }

    @Override
    public List<User> getFollowedByUidAndKeyword(int uId, String keyword) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession(true);
       // FollowDao dao=sqlSession.getMapper(FollowDao.class);
        List<User> list=followDao.getFollowedByUidAndKeyword(uId, keyword);
        //sqlSession.commit();
        //sqlSession.close();
        return list;
    }

    @Override
    public List<User> getFansByUidAndKeyword(int uId, String keyword) throws SQLException {
        //SqlSession sqlSession=sessionFactory.openSession(true);
       // FollowDao dao=sqlSession.getMapper(FollowDao.class);
        List<User> list=followDao.getFansByUidAndKeyword(uId, keyword);
       // sqlSession.commit();
        //sqlSession.close();
        return list;
    }

    @Override
    public boolean isFollowed(int uId, int followedUid) throws SQLException {
       // SqlSession sqlSession=sessionFactory.openSession(true);
       // FollowDao followDao=sqlSession.getMapper(FollowDao.class);
        Boolean is=followDao.isFollowed(uId, followedUid);
       // sqlSession.commit();
        //sqlSession.close();
        return  is;
    }

    @Override
    public int addFollower(Follow follow) throws SQLException {
//        Connection conn = null;
//        int result =0;
//        try{
//            conn  = DatabaseUtil.getConnection();
//            FollowDao followDao = new FollowDaoImpl(conn);
//            if(!followDao.isFollowed(follow.getuId(),follow.getFollowerId())){
//                result = followDao.addFollower(follow);
//            }else {
//                result = -1;
//            }
//        }catch(SQLException e){
//            e.printStackTrace();
//            throw e;
//        }finally{
//            DatabaseUtil.closeAll(conn,null,null);
//        }
//        return result;
        int ret=followDao.addFollower(follow);
        return ret;
    }

    @Override
    public int deleteFollower(Follow follow) throws SQLException {
//        Connection conn = null;
//        int result =0;
//        try{
//            conn  = DatabaseUtil.getConnection();
//            FollowDao followDao = new FollowDaoImpl(conn);
//            conn.setAutoCommit(false);
//            if(followDao.isFollowed(follow.getuId(),follow.getFollowerId())){
//                result = followDao.deleteFollower(follow);
//            }
//            conn.commit();
//        }catch(SQLException e){
//            e.printStackTrace();
//            if (conn != null)
//                try {
//                    conn.rollback();
//                } catch (SQLException e1) {
//                    e1.printStackTrace();
//                }
//            throw e;
//        }finally{
//            DatabaseUtil.closeAll(conn,null,null);
//        }
//        return result;
        int ret=followDao.deleteFollower(follow);
        return ret;
    }
}
